/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.user.service.impl;


import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.domain.PageResult;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.user.domain.ShoppingPayform;
import co.yixiang.modules.user.service.ShoppingPayformService;
import co.yixiang.modules.user.service.dto.ShoppingPayformDto;
import co.yixiang.modules.user.service.dto.ShoppingPayformQueryCriteria;
import co.yixiang.modules.user.service.mapper.ShoppingPayformMapper;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2020-11-26
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "shoppingPayform")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShoppingPayformServiceImpl extends BaseServiceImpl<ShoppingPayformMapper, ShoppingPayform> implements ShoppingPayformService {

    @Autowired
    private ShoppingPayformMapper shoppingPayformMapper;
    private final IGenerator generator;


    @Override
    //@Cacheable
    public PageResult<ShoppingPayformDto> queryAll(ShoppingPayformQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ShoppingPayform> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page, ShoppingPayformDto.class);
    }


    @Override
    //@Cacheable
    public List<ShoppingPayform> queryAll(ShoppingPayformQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ShoppingPayform.class, criteria));
    }


    @Override
    public void download(List<ShoppingPayformDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShoppingPayformDto shoppingPayform : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" uid",  shoppingPayform.getUid());
            map.put(" addtime",  shoppingPayform.getAddtime());
            map.put(" deletestatus",  shoppingPayform.getDeletestatus());
            map.put(" totalprice",  shoppingPayform.getTotalprice());
            map.put(" payno",  shoppingPayform.getPayno());
            map.put("银联支付单号", shoppingPayform.getBillno());
            map.put("支付状态", shoppingPayform.getPaystatus());
            map.put("退款原单支付单", shoppingPayform.getPfId());
            map.put("银联原支付单", shoppingPayform.getOldbillno());
            map.put("退款订单", shoppingPayform.getOfId());
            map.put("退款类型", shoppingPayform.getPfType());
            map.put("批次号", shoppingPayform.getBatchno());
            map.put("积分订单ID", shoppingPayform.getIgoOfId());
            map.put("订单号", shoppingPayform.getOrderId());
            map.put("支付方式", shoppingPayform.getPayType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 订单信息
     * @param payno 唯一值或者单号
     * @return ShoppingPayform
     */
    @Override
    public ShoppingPayform getPayform4no(String payno) {
        ShoppingPayform pf = new ShoppingPayform();
        //pf.setPayno(payno);
        pf = shoppingPayformMapper.selectOne(Wrappers.<ShoppingPayform>lambdaQuery()
                .eq(ShoppingPayform::getPayno, payno));
        //pf =shoppingPayformMapper.selectById(vo.getId());
        return pf;
    }
}
